package cn.qhuan.modules.controller;

import cn.qhuan.modules.dtos.DynastyDto;
import cn.qhuan.modules.dtos.DynastyEnableDto;
import cn.qhuan.modules.entity.Dynasty;
import cn.qhuan.modules.service.IDynastyService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
* @Description: 朝代表
* @Author: 司翔宇
* @Date:   2020-12-18
* @Version: V1.0
*/
@Api(tags="朝代表")
@RestController
@RequestMapping("/dynasty")
@Slf4j
public class DynastyController extends JeecgController<Dynasty, IDynastyService> {
   @Autowired
   private IDynastyService dynastyService;

    /**
     * 条件分页查询
     *
     * @param dto
     * @return
     */
    @AutoLog(value = "朝代表-通过条件查询")
    @ApiOperation(value = "朝代表-通过条件查询", notes = "朝代表-通过条件查询")
    @PostMapping("/list")
    public Result list(@RequestBody DynastyDto dto) {
        return dynastyService.queryByKeyWord(dto);
    }

    /**
     * 查询全部
     * @return
     */
    @AutoLog(value = "朝代表-查询全部")
    @ApiOperation(value="朝代表-查询全部", notes="朝代表-查询全部")
    @GetMapping(value = "/listAll")
   public Result<?> findAll(){
       List<Dynasty> list = dynastyService.list();
       return Result.OK(list);
   }
   /**
    *   添加
    *
    * @param qhDynasty
    * @return
    */
   @AutoLog(value = "朝代表-添加")
   @ApiOperation(value="朝代表-添加", notes="朝代表-添加")
   @PostMapping(value = "/add")
   public Result<?> add(@RequestBody Dynasty qhDynasty) {
       dynastyService.save(qhDynasty);
       return Result.OK("添加成功！");
   }

   /**
    *  编辑
    *
    * @param qhDynasty
    * @return
    */
   @AutoLog(value = "朝代表-编辑")
   @ApiOperation(value="朝代表-编辑", notes="朝代表-编辑")
   @PutMapping(value = "/edit")
   public Result<?> edit(@RequestBody Dynasty qhDynasty) {
       dynastyService.updateById(qhDynasty);
       return Result.OK("编辑成功!");
   }

   /**
    *   通过id删除
    *
    * @param id
    * @return
    */
   @AutoLog(value = "朝代表-通过id删除")
   @ApiOperation(value="朝代表-通过id删除", notes="朝代表-通过id删除")
   @DeleteMapping(value = "/delete")
   public Result<?> delete(@RequestParam(name="id",required=true) String id) {
       dynastyService.removeById(id);
       return Result.OK("删除成功!");
   }

   /**
    *  批量删除
    *
    * @param ids
    * @return
    */
   @AutoLog(value = "朝代表-批量删除")
   @ApiOperation(value="朝代表-批量删除", notes="朝代表-批量删除")
   @DeleteMapping(value = "/deleteBatch")
   public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
       this.dynastyService.removeByIds(Arrays.asList(ids.split(",")));
       return Result.OK("批量删除成功!");
   }

   /**
    * 通过id查询
    *
    * @param id
    * @return
    */
   @AutoLog(value = "朝代表-通过id查询")
   @ApiOperation(value="朝代表-通过id查询", notes="朝代表-通过id查询")
   @GetMapping(value = "/queryById")
   public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
       Dynasty qhDynasty = dynastyService.getById(id);
       if(qhDynasty==null) {
           return Result.error("未找到对应数据");
       }
       return Result.OK(qhDynasty);
   }
    /**
     * 朝代是否使用
     * @param dto
     * @return
     */
    @AutoLog(value = "朝代表-朝代是否使用")
    @ApiOperation(value = "朝代表-朝代是否使用", notes = "朝代表-朝代是否使用")
    @PostMapping("/enable")
    public Result enableDynasty(@RequestBody DynastyEnableDto dto,Integer type){
        return dynastyService.updateEnableStatus(dto,type);
    }

   /**
   * 导出excel
   *
   * @param request
   * @param qhDynasty
   */
   @RequestMapping(value = "/exportXls")
   public ModelAndView exportXls(HttpServletRequest request, Dynasty qhDynasty) {
       return super.exportXls(request, qhDynasty, Dynasty.class, "朝代表");
   }

   /**
     * 通过excel导入数据
   *
   * @param request
   * @param response
   * @return
   */
   @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
   public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
       return super.importExcel(request, response, Dynasty.class);
   }

}
